package com.derbysoft.dswitch.adapter.ihg.common.service.dswitch3;

import com.derbysoft.dswitch.adapter.ihg.common.domain.FreeMealStrategy;
import com.derbysoft.dswitch.adapter.ihg.common.domain.HotelMapping;
import com.derbysoft.dswitch.adapter.ihg.common.service.AvailRoomStayEnhancer;
import com.derbysoft.dswitch.adapter.ihg.common.service.FreeMealMatchService;
import com.derbysoft.dswitch.dto.hotel.avail.AvailRoomStayDTO;
import com.derbysoft.dswitch.dto.hotel.avail.FreeMealDTO;
import com.derbysoft.dswitch.dto.hotel.avail.HotelAvailRoomStayDTO;
import com.derbysoft.remote.dswitch.hotel.constants.FreeMealType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * User: jason
 * Date: 2013-01-06
 */
@Component("ds3AvailRoomStayEnhancer")
public class DS3AvailRoomStayEnhancer implements AvailRoomStayEnhancer<HotelAvailRoomStayDTO> {
    @Autowired
    private FreeMealMatchService freeMealMatchService;

    @Override
    public void enhance(HotelAvailRoomStayDTO hotelAvailRoomStayDTO, String channel, HotelMapping hotelMapping) {
        for (AvailRoomStayDTO availRoomStayDTO : hotelAvailRoomStayDTO.getRoomStaysList()) {
            String ratePlanCode = availRoomStayDTO.getRatePlan().getCode();
            String roomTypeCode = availRoomStayDTO.getRoomType().getCode();
            FreeMealStrategy freeMealStrategy = freeMealMatchService.match(channel, hotelMapping.getCooperatorCode(), ratePlanCode, roomTypeCode);
            if (freeMealStrategy == null) {
                continue;
            }
            setFreeMeal(freeMealStrategy, availRoomStayDTO);
        }
    }

    private void setFreeMeal(FreeMealStrategy freeMealStrategy, AvailRoomStayDTO availRoomStayDTO) {
        FreeMealDTO freeMealDTO = new FreeMealDTO();
        freeMealDTO.setType(translateFreeMealType(freeMealStrategy.getType()));
        freeMealDTO.setQuantity(freeMealStrategy.getQuantity());
        freeMealDTO.setDescription(freeMealStrategy.getDescription());
        availRoomStayDTO.getRatePlan().setFreeMeal(freeMealDTO);
    }

    private FreeMealDTO.FreeMealType translateFreeMealType(FreeMealType type) {
        if (type == FreeMealType.BREAKFAST) {
            return FreeMealDTO.FreeMealType.BREAKFAST;
        }
        if (type == FreeMealType.BREAKFAST_AND_DINNER) {
            return FreeMealDTO.FreeMealType.BREAKFAST_AND_DINNER;
        }
        if (type == FreeMealType.BREAKFAST_AND_LUNCH_AND_DINNER) {
            return FreeMealDTO.FreeMealType.BREAKFAST_AND_LUNCH_AND_DINNER;
        }
        if (type == FreeMealType.ON_RATE) {
            return FreeMealDTO.FreeMealType.ON_RATE;
        }
        if (type == FreeMealType.UNKNOWN) {
            return FreeMealDTO.FreeMealType.NONE;
        }
        return FreeMealDTO.FreeMealType.NONE;
    }
}
